package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.CarInfoListResponse
import com.gexiaobao.pigeon.app.model.bean.DrippingBaseInfoResponse
import com.gexiaobao.pigeon.app.model.bean.FeatherColorResponse
import com.gexiaobao.pigeon.app.model.bean.OrderDrippingDetailResponse
import com.gexiaobao.pigeon.app.model.bean.OrderTakingResponseList
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.param.GetVerCodeParams
import com.gexiaobao.pigeon.app.model.param.ShedSignParam
import com.gexiaobao.pigeon.app.model.param.UpDateLoftPigeonParam
import com.gexiaobao.pigeon.app.model.param.UserSignParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.SettingUtil
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import okhttp3.RequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/6/24 13:59
 * @Describe    :
 */
class PigeonCatcherViewModel : BaseViewModel() {

    private var pageNo: Int = 1
    private val pageSize: Int = 20

    /** 获取订单列表（接鸽员角色）  */
    val orderListResult: MutableLiveData<ListDataUiState<OrderTakingResponseList>> = MutableLiveData()
    fun getOrderDidiListWithTransporter(isRefresh: Boolean, orgId: Int, status: Int, mLongitude: Double, mLatitude: Double) {
        if (isRefresh) {
            pageNo = 1
        }
        request({ apiService.getOrderDidiListWithTransporter(pageNo, pageSize, orgId, status, mLongitude, mLatitude) }, {
            pageNo++
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = isRefresh,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = isRefresh && it.isEmpty(),
                listData = it.list,
            )
            orderListResult.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(
                isSuccess = false,
                isRefresh = isRefresh,
                errCode = it.code,
                errMessage = it.msg,
                listData = arrayListOf<OrderTakingResponseList>()
            )
            orderListResult.value = listDataUiState
            RxToast.warning(it.msg)
        })
    }

    /** 获取订单详情 */
    val drippingOrderDetailResult: MutableLiveData<OrderDrippingDetailResponse> = MutableLiveData()
    fun getOrderDidiDetail(id: String) {
        request({ apiService.getOrderDidiDetail(id) }, {
            drippingOrderDetailResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 接订单/指定订单接收（订单状态变更）  */
    val receiveOrderResult: MutableLiveData<Any> = MutableLiveData()
    fun receiveOrderDidi(body: RequestBody) {
        request({ apiService.receiveOrderDidi(body) }, {
            receiveOrderResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 订单（订单状态变更）(接鸽员)  */
    val returnOrderResult: MutableLiveData<Any> = MutableLiveData()
    fun returnOrderDidi(body: RequestBody) {
        request({ apiService.returnOrderDidi(body) }, {
            returnOrderResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 发送验证码 */
    val verCodeResult: MutableLiveData<UiState> = MutableLiveData()
    fun getVerCode(body: GetVerCodeParams) {
        request({ apiService.getVerCode(body) }, {
            val result = UiState(isSuccess = true)
            verCodeResult.value = result
        }, {
            val result = UiState(isSuccess = false, errorMsg = it.msg)
            verCodeResult.value = result
        })
    }

    /** 查询接鸽员申请中关于个人的信息查询（用户角色）  */
    var drippingBaseInfoResult: MutableLiveData<DrippingBaseInfoResponse> = MutableLiveData()
    fun getTransporterApplyBaseByUserId() {
        request({ apiService.getTransporterApplyBaseByUserId() }, {
            drippingBaseInfoResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 查询接鸽员申请中关于汽车的信息列表  */
    var carInfoListResult: MutableLiveData<CarInfoListResponse> = MutableLiveData()
    fun getTransporterApplyCarList() {
        request({ apiService.getTransporterApplyCarList() }, {
            carInfoListResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 查询接鸽员申请中关于汽车的信息列表  */
    var deleteCarResult: MutableLiveData<Any> = MutableLiveData()
    fun deleteTransporterApplyCar(id: String) {
        request({ apiService.deleteTransporterApplyCar(id) }, {
            deleteCarResult.value = it
        }, {
            RxToast.warning(it.msg)
        })
    }

    /** 接鸽（接鸽员角色）（鸽友签字确认）（订单状态变更） */
    val userAndTranSignResult: MutableLiveData<UiState> = MutableLiveData()
    fun receivePigeonWithOrderDidi(param: UserSignParam) {
        val userFile = param.signUser
        val transporterFile = param.signTransporter

        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()

        hashMap["id"] = param.id.toString()
        hashMap["code"] = param.code
        hashMap["mobile"] = param.mobile

        if (userFile != null) {
            fileList["signUser"] = userFile
        } else {
            hashMap["signUser"] = ""
        }

        if (transporterFile != null) {
            fileList["signTransporter"] = transporterFile
        } else {
            hashMap["signTransporter"] = ""
        }
        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.receivePigeonWithOrderDidi(body) }, {
            val deleteState = UiState(isSuccess = true)
            userAndTranSignResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            userAndTranSignResult.value = deleteState
        })
    }

    /** 依据接鸽清单收鸽（接鸽员角色）（公棚签字确认）（订单状态变更） */
    val shedSignResult: MutableLiveData<UiState> = MutableLiveData()
    fun completeOrderDidi(param: ShedSignParam) {
        val orgFile = param.signOrg

        val fileList = HashMap<String, File?>()
        val hashMap = HashMap<String, String>()

        hashMap["id"] = param.id.toString()
        hashMap["code"] = param.code
        hashMap["mobile"] = param.mobile

        if (orgFile != null) {
            fileList["signOrg"] = orgFile
        } else {
            hashMap["signOrg"] = ""
        }

        val body = SettingUtil.filesToMultipartBody(fileList, hashMap)
        request({ apiService.completeOrderDidi(body) }, {
            val deleteState = UiState(isSuccess = true)
            shedSignResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            shedSignResult.value = deleteState
        })
    }

}